package com.deercrowing.takeaway.controller.user;


import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.entity.AddressEntity;
import com.deercrowing.takeaway.service.AddressService;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 地址表(Address)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-19 09:40:29
 */
@Slf4j
@Validated
@RestController
@RequestMapping("user/address")
public class AddressController {
    /**
     * 服务对象
     */
    @Autowired
    private AddressService addressService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseEntity selectAll() {
        log.info("用户地址信息:{}",this.addressService.searchByUserId(BaseContext.getCurrentId()));
        return ResponseEntity.ok(Result.OK(this.addressService.searchByUserId(BaseContext.getCurrentId())));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public ResponseEntity selectOne(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        if (ObjectUtils.isEmpty(this.addressService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.addressService.getById(id)));
    }

    /**
     * 新增数据
     *
     * @param addressEntity 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity insert(@RequestBody AddressEntity addressEntity) {
        log.info("地址信息:{}",addressEntity);
        if (ObjectUtils.isEmpty(addressEntity)){
            throw new NullPointerException();
        }

        this.addressService.saveInfo(addressEntity);

        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改数据
     *
     * @param id 主键, addressEntity 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable @NotNull(message = "主键不能为空") Long id, @RequestBody AddressEntity addressEntity) {
        if (ObjectUtils.isEmpty(this.addressService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.addressService.updateById(addressEntity)));
    }
    
    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @DeleteMapping("/{id}")
    public ResponseEntity delete(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        if (ObjectUtils.isEmpty(this.addressService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.addressService.removeById(id)));
    }

    /**
     * 设置默认地址
     *
     * @param addressEntity
     * @return
     */
    @PutMapping("/setdefault")
    public ResponseEntity setDefaultAddress(@RequestBody AddressEntity addressEntity){
        if (ObjectUtils.isEmpty(addressEntity) || addressEntity == null) {
            throw new NullPointerException();
        }

        this.addressService.setDefaultAddress(addressEntity);
        return ResponseEntity.ok(Result.OK());
    }

    @GetMapping("/default")
    public ResponseEntity getDefaultAddress(){
        return ResponseEntity.ok(Result.OK(this.addressService.searchDefaultAddressByUserId(BaseContext.getCurrentId())));
    }
}

